//
//  WelcomeController.swift
//  swiftImitationWeibo
//
//  Created by 杨果果 on 16/7/12.
//  Copyright © 2016年 yang. All rights reserved.
//

import UIKit
import SDWebImage
class WelcomeController: UIViewController {
    private lazy var bgImageVIew: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "ad_background"))
        return imageView
    }()
    private lazy var headImageView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        if let imageUrl = UserAccountViewModel.sharedUserAccountViewModel.userAccount?.avatar_large{
            imageView.sd_setImageWithURL(NSURL(string: imageUrl), placeholderImage: UIImage(named: "avatar_default_big"))
        }
        imageView.layer.cornerRadius = 42.5
        imageView.layer.masksToBounds = true
        return imageView
    
    }()
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFontOfSize(15)
        label.textColor = UIColor.grayColor()
        
        if let name = UserAccountViewModel.sharedUserAccountViewModel.userAccount?.name{
            label.text = "欢迎回来，\(name)"
        } else {
        label.text = "欢迎回来"
        }
        label.alpha = 0
        return label
    }()
    
    override func loadView() {
        view = bgImageVIew
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    //约束
    private func setupUI() {
        view.addSubview(headImageView)
        view.addSubview(messageLabel)
        headImageView.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(view).offset(200)
            make.centerX.equalTo(view)
            make.size.equalTo(CGSize(width: 85, height: 85))
            
        }
        messageLabel.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(headImageView.snp_bottom).offset(20)
            make.centerX.equalTo(headImageView)
        }
    }
    //动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        startAnimation()
    }
    private func startAnimation() {
        self.headImageView.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(self.view).offset(100)
        }
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(1, animations: { () -> Void in
                    self.messageLabel.alpha = 1
                    },completion: { (_) -> Void in
                    NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootVCNotification, object: nil)
                })
        }
    }
}
